package com.base.cn.platform.os.controller.course.mould;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.mould.CouMould;
import com.base.cn.platform.os.entity.course.mould.CouMouldCondition;
import com.base.cn.platform.os.service.course.mould.CouMouldBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模块Controller接口
 *
 * @author s.li
 * @create 2018-04-23-19:41
 */
@RestController
@RequestMapping("/course")
public class MouldController extends BaseController {

    @Autowired
    private CouMouldBiz couMouldBiz;

    /**
     * 查询模块数量
     * @param couMouldCondition
     * @return 数量
     */
    @RequestMapping("/mouldCount")
    public BigDecimal mouldCount(@RequestBody CouMouldCondition couMouldCondition){
        return couMouldBiz.mouldCount(couMouldCondition);
    }

    /**
     * 保存模块数据
     * @param couMould 模块数据对象
     * @return  CouMould
     */
    @RequestMapping("/saveMould")
    public Map<String,Object> saveMould(@RequestBody CouMould couMould){
        if(StringUtils.isEmpty(couMould.getMouldName())){
            return ResultUtil.ERROR("请填写模块名称");
        }else{
            if(couMould.getMouldName().trim().length()>30){
                return ResultUtil.ERROR("模块名称限制在30个字以内");
            }
        }
        String message = StringUtils.checkSubjectCondition(couMould.getSubjectIds());
        if(message !=null){
            return ResultUtil.ERROR(message);
        }
        couMould = couMouldBiz.saveMould(couMould);
        return ResultUtil.SUCCESS("模块数据保存成功",couMould);
    }

    /**
     * 修改模块状态
     * @param ids
     * @param status 状态，1未发布，2已发布，3隐藏，4删除，5显示
     * @return Map<String,Object>
     */
    @RequestMapping("/updateCouMouldStatus")
    public Map<String,Object> updateCouMouldStatus(String ids,int status){
        List<CouMould> couMouldList = couMouldBiz.findCouMouldByIds(ids,false,false,false);
        if(ObjectUtils.isNotEmpty(couMouldList)){
            List<CouMould> updateList = new ArrayList<>();
            List<CouMould> deleteList = new ArrayList<>();
            Map<Integer,List<CouMould>> couMouldListMaps = couMouldList.stream().collect(Collectors.groupingBy(e->e.getStatus()));
            List<CouMould> couMouldList1 = couMouldListMaps.get(1);//未发布
            List<CouMould> couMouldList2 = couMouldListMaps.get(2);//已发布
            List<CouMould> couMouldList3 = couMouldListMaps.get(3);//隐藏
            if(status ==2 || status ==5){//如果是发布或恢复
                if(ObjectUtils.isNotEmpty(couMouldList1) && status==2){
                    updateList.addAll(couMouldList1);
                }
                if(ObjectUtils.isNotEmpty(couMouldList3)){
                    updateList.addAll(couMouldList3);
                }
            }
            if(status==3){//隐藏
                if(ObjectUtils.isNotEmpty(couMouldList2)){
                    updateList.addAll(couMouldList2);
                }
            }
            if(status==4){//删除
                if(ObjectUtils.isNotEmpty(couMouldList1)){
                    deleteList.addAll(couMouldList1);
                }
                if(ObjectUtils.isNotEmpty(couMouldList2)){
                    updateList.addAll(couMouldList2);
                }
                if(ObjectUtils.isNotEmpty(couMouldList3)){
                    updateList.addAll(couMouldList3);
                }
            }
            couMouldBiz.updateCouMouldStatus(updateList,deleteList,status);
        }
        String message = "";
        if(status==2){
            message = "模块发布成功";
        }else if(status==3){
            message = "模块隐藏成功";
        }else if(status==4){
            message = "模块删除成功";
        }else if(status==5){//如果是显示
            message = "模块显示成功";
        }
        return ResultUtil.SUCCESS(message);
    }

    /**
     * 设置模块完成
     * @param ids
     * @return Map<String,Object>
     */
    @RequestMapping("/updateMouldFinishStatus")
    public Map<String,Object> updateMouldFinishStatus(String ids){
        couMouldBiz.updateMouldFinishStatus(ids);
        return ResultUtil.SUCCESS("模块数据设置完成成功");
    }

    /**
     * 通过ID查询模块对象数据
     * @param id
     * @param subjectData
     * @param createUserData
     * @param materialData
     * @return
     */
    @RequestMapping("/findCouMouldById")
    public CouMould findCouMouldById(BigDecimal id,
                                     @RequestParam("subjectData") boolean subjectData,
                                     @RequestParam("createUserData") boolean createUserData,
                                     @RequestParam("materialData") boolean materialData){
        return couMouldBiz.findCouMouldById(id,subjectData,createUserData,materialData);
    }

    /**
     * 通过ID串，查询模块列表
     * @param ids
     * @param subjectData
     * @param createUserData
     * @param materialData
     * @return
     */
    @RequestMapping("/findCouMouldByIds")
    public List<CouMould> findCouMouldByIds(String ids,
                                            @RequestParam("subjectData") boolean subjectData,
                                            @RequestParam("createUserData") boolean createUserData,
                                            @RequestParam("materialData") boolean materialData){
        return  couMouldBiz.findCouMouldByIds(ids,subjectData,createUserData,materialData);
    }

    /**
     * 多条件是查询模块列表
     * @param condition 查询条件
     * @param subjectData 设置专业数据
     * @param createUserData 设置创建人数据
     * @param materialData 设置素材数据
     * @return List<CouMould>
     */
    @RequestMapping("/findCouMouldList")
    public List<CouMould> findCouMouldList(@RequestBody CouMouldCondition condition,
                                           @RequestParam("subjectData") boolean subjectData,
                                           @RequestParam("createUserData") boolean createUserData,
                                           @RequestParam("materialData") boolean materialData){
        return couMouldBiz.findCouMouldList(condition,subjectData,createUserData,materialData);
    }

    /**
     * 分页查询模块数据
     * @param condition 查询条件
     * @param subjectData 设置专业数据
     * @param createUserData 设置创建人数据
     * @param materialData 设置素材数据
     * @return PageInfo<CouMould>
     */
    @RequestMapping("/findCouMouldPage")
    public PageInfo<CouMould> findCouMouldPage(@RequestBody CouMouldCondition condition,
                                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                               @RequestParam("subjectData") boolean subjectData,
                                               @RequestParam("createUserData") boolean createUserData,
                                               @RequestParam("materialData") boolean materialData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(14);
       return couMouldBiz.findCouMouldPage(condition,page,subjectData,createUserData,materialData);
    }
}
